/*
 * 
 */
package siml;

import java.awt.Event;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Map;
import java.util.Properties;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.tree.DefaultTreeModel;
import org.openswing.swing.domains.java.Domain;
import org.openswing.swing.internationalization.java.EnglishOnlyResourceFactory;
import org.openswing.swing.internationalization.java.Language;
import org.openswing.swing.mdi.client.ClientFacade;
import org.openswing.swing.mdi.client.Clock;
import org.openswing.swing.mdi.client.GenericStatusPanel;
import org.openswing.swing.mdi.client.MDIController;
import org.openswing.swing.mdi.client.MDIFrame;
import org.openswing.swing.mdi.java.ApplicationFunction;
import org.openswing.swing.permissions.client.LoginController;
import org.openswing.swing.permissions.client.LoginDialog;
import org.openswing.swing.util.client.ClientSettings;

/**
 *
 * @author sugik
 */
public class MainMenu implements MDIController, LoginController {

    private Connection conn = null;
    private SIMLClientFacade clientFacade = null;
    private Hashtable domains = new Hashtable();
    private String username;

    public MainMenu(String[] argv, Connection conn) {
        Properties props = new Properties();
        this.conn = conn;
        clientFacade = new SIMLClientFacade(conn);
        ClientSettings clientSettings = new ClientSettings(
                new EnglishOnlyResourceFactory("E", props, true),
                new Hashtable());

        ClientSettings.BACKGROUND = "background3.jpg";
        ClientSettings.TREE_BACK = "treeback2.jpg";
        ClientSettings.AUTO_EXPAND_TREE_MENU = true;
        ClientSettings.MIN_MENU_WIDTH = 300;
        ClientSettings.DIVIDER_WIDTH = 0;

        if (argv.length == 1) {
            ClientSettings.LOOK_AND_FEEL_CLASS_NAME = argv[0];
        }
        //MDIFrame mdi = new MDIFrame(this);
        LoginDialog d = new LoginDialog(null, false, this);
    }

    public void afterMDIcreation(MDIFrame frame) {
        GenericStatusPanel userPanel = new GenericStatusPanel();
        userPanel.setColumns(12);
        MDIFrame.addStatusComponent(userPanel);
        userPanel.setText(username);
        MDIFrame.addStatusComponent(new Clock());
        /*frame.addButtonToToolBar("new.gif", "New Record");
        frame.addButtonToToolBar("edit.gif", "Edit Record");
        frame.addButtonToToolBar("reload.gif", "Undo/Refresh Record");
        frame.addButtonToToolBar("save.gif", "Save Record");
        frame.addButtonToToolBar("del.gif", "Delete Record");
        frame.setBorderPainterOnToolBar(false);
        frame.setFloatableOnToolBar(false);
        frame.setRolloverOnToolBar(false);
        new SplashScreen(frame, "about.jpg", getMDIFrameTitle(), 5);
        frame.getMenuItem("F2").setMnemonic('2');*/

//    JPanel rightPanel = new JPanel();
//    rightPanel.setBorder(BorderFactory.createTitledBorder("Right Panel"));
//    rightPanel.setPreferredSize(new Dimension(150,frame.getHeight()));
//    frame.getContentPane().add(rightPanel,BorderLayout.EAST);

    }

    /**
     * @see JFrame getExtendedState method
     */
    public int getExtendedState() {
        return JFrame.MAXIMIZED_BOTH;
    }

    /**
     * Method used to destroy application.
     */
    public void stopApplication() {
        System.exit(0);
    }

    /**
     * Defines if application functions must be viewed inside a tree panel of MDI Frame.
     * @return <code>true</code> if application functions must be viewed inside a tree panel of MDI Frame, <code>false</code> no tree is viewed
     */
    public boolean viewFunctionsInTreePanel() {
        return true;
    }

    /**
     * Defines if application functions must be viewed in the menubar of MDI Frame.
     * @return <code>true</code> if application functions must be viewed in the menubar of MDI Frame, <code>false</code> otherwise
     */
    public boolean viewFunctionsInMenuBar() {
        return true;
    }

    /**
     * @return <code>true</code> if the MDI frame must show a login menu in the menubar, <code>false</code> no login menu item will be added
     */
    public boolean viewLoginInMenuBar() {
        return false;
    }

    /**
     * @return application title
     */
    public String getMDIFrameTitle() {
        return "SIML";
    }

    /**
     * @return text to view in the about dialog window
     */
    public String getAboutText() {
        return "<html><body><p style='font-family: Arial,sans-serif;font-size:12'>Aplikasi SIM Loundry<br>"
                + "<br>"
                + "Copyright: Copyright (C) 2011 Sugik Puja Kusuma [mbahsomo]<br>"
                + "<a href='http://www.do-event.com'>DoEvent</a><br>"
                + "<a href='http://sugik.do-event.com'>My Blog</a><br>"
                + "Author: mbahsomo</body></html>";
    }

    /**
     * @return image name to view in the about dialog window
     */
    public String getAboutImage() {
        return "about.jpg";
    }

    /**
     * @param parentFrame parent frame
     * @return a dialog window to logon the application; the method can return null if viewLoginInMenuBar returns false
     */
    public JDialog viewLoginDialog(JFrame parentFrame) {
        JDialog d = new LoginDialog(parentFrame, true, this);
        return d;
    }

    /**
     * @return maximum number of failed login
     */
    public int getMaxAttempts() {
        return 0;
    }

    /**
     * Method called by MDI Frame to authenticate the user.
     * @param loginInfo login information, like username, password, ...
     * @return <code>true</code> if user is correcly authenticated, <code>false</code> otherwise
     */
    public boolean authenticateUser(Map loginInfo) throws Exception {
        return true;
    }

    /**
     * Method called by LoginDialog to notify the sucessful login.
     * @param loginInfo login information, like username, password, ...
     */
    public void loginSuccessful(Map loginInfo) {
        username = loginInfo.get("username").toString().toUpperCase();
        if (username.equals("ADMIN")) {
            ClientSettings.getInstance().setLanguage("EN");
            Domain orderStateDomain = new Domain("ORDERSTATE");
            orderStateDomain.addDomainPair("O", "opened");
            orderStateDomain.addDomainPair("S", "suspended");
            orderStateDomain.addDomainPair("D", "delivered");
            orderStateDomain.addDomainPair("ABC", "closed");
            domains.clear();
            domains.put(
                    orderStateDomain.getDomainId(),
                    orderStateDomain);

            MDIFrame mdi = new MDIFrame(this);
            try {
              UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
              SwingUtilities.updateComponentTreeUI(mdi);
            }
            catch (Throwable ex) {
              ex.printStackTrace();
            }
        } else if (username.equals("MAURO")) {
            ClientSettings.getInstance().setLanguage("IT");
        } else{
            System.exit(0);
        }
        
    }

    /**
     * @return <code>true</code> if the MDI frame must show a change language menu in the menubar, <code>false</code> no change language menu item will be added
     */
    public boolean viewChangeLanguageInMenuBar() {
        return false;
    }

    /**
     * @return list of languages supported by the application
     */
    public ArrayList getLanguages() {
        ArrayList list = new ArrayList();
        list.add(new Language("EN", "English"));
        return list;
    }

    /**
     * @return application functions (ApplicationFunction objects), organized as a tree
     */
    public DefaultTreeModel getApplicationFunctions() {
        ApplicationFunction root = new ApplicationFunction("Function0", "F0", null, "getF0");
        DefaultTreeModel model = new DefaultTreeModel(root);

        //Buat Folder Induk
        ApplicationFunction n1 = new ApplicationFunction("Master", null);
        ApplicationFunction n2 = new ApplicationFunction("Transaksi", null);
        ApplicationFunction n3 = new ApplicationFunction("Laporan", null);

        //Buat Anak
        ApplicationFunction n1_1 = new ApplicationFunction("User", "F1", null, "getUser");
        //n11.setShortCut(new Character('1'));
        ApplicationFunction n1_2 = new ApplicationFunction("Barang", "F1", null, "getBarang");
        //Buat anak
        ApplicationFunction n21 = new ApplicationFunction("Pencucian", "F2", null, "getF2");
        n21.setAccelerator(KeyStroke.getKeyStroke('F', Event.CTRL_MASK));
        //Buat Anak
        ApplicationFunction n31 = new ApplicationFunction("Laporan", "F4", null, "getF4");

        n1.add(n1_1);
        n2.add(n21);
        n3.add(n31);

        root.add(n1);
        root.add(n2);
        root.add(n3);

        return model;
    }

    /**
     * @return <code>true</code> if the MDI frame must show a panel in the bottom, containing last opened window icons, <code>false</code> no panel is showed
     */
    public boolean viewOpenedWindowIcons() {
        return true;
    }

    /**
     * @return <code>true</code> if the MDI frame must show the "File" menu in the menubar of the frame, <code>false</code> to hide it
     */
    public boolean viewFileMenu() {
        return true;
    }

    @Override
    public ClientFacade getClientFacade() {
        return clientFacade;
    }
}
